<?php
/**
 * Common_Limit 接口限制器
 *
 * - 接口限制方案的方案待定：
 *
 *
 *
 *
 *
 * @author      andy <www@webx32.com> 2016-03-22
 */

class Common_Limit implements PhalApi_Filter {


    public function __construct() {

    }

    public function check() {
        $allParams = DI()->request->getAll();
        if (empty($allParams)) {
            return;
        }
        /**
         * 接口计数
         */
        DI()->cache->get_incr("count:".$allParams['service']);


        /**
         * token自动验证
         */
        //服务名称
        $service="Api_".substr($allParams['service'],0,strpos($allParams['service'],'.'));
        //服务方法
        $fn=substr($allParams['service'],strpos($allParams['service'],'.')+1);

        $s=new $service();
        $tokenCtrl = new Domain_Token();
        $rules=$s->getRules();
        $mustToken=isset($rules["*"])&&isset($rules["*"]['token'])&&$rules["*"]['token']['require']||isset($rules[lcfirst($fn)])&&isset($rules[lcfirst($fn)]['token'])&&$rules[lcfirst($fn)]['token']['require'];
        if($mustToken){

            $tokenCtrl->check($allParams['token']);
            /**
             * 自动更新token有效时间
             */
            DI()->cache->setTimeout("userInfo:".$allParams['token'],60*60*2);
        }






    }


}